package com.xyy.saas.payment.callback;

import com.alibaba.dubbo.config.ReferenceConfig;
import com.xyy.saas.payment.cores.api.BusinessPayResultApi;
import com.xyy.saas.payment.cores.api.CallbackResultApi;
import com.xyy.saas.payment.cores.api.PaymentResultApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class CallBackRpcManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(CallBackRpcManager.class);

    private static ConcurrentHashMap<String, PaymentResultApi> paymentResultServiceMap = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String, CallbackResultApi> callbackResultApiMap = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String, BusinessPayResultApi> businessPayResultApiMap = new ConcurrentHashMap<>();

    private static String VERSION = "1.0.0";

    private  <T>T getInvokeService(Class<?> interfaceClass, String version, String group) {
        ReferenceConfig<T> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setVersion(version);
        referenceConfig.setGroup(group);
        referenceConfig.setInterface(interfaceClass);
        return referenceConfig.get();
    }

    public PaymentResultApi getPaymentResultService(String group) {
        PaymentResultApi paymentResultApi = paymentResultServiceMap.get(group);
        if(paymentResultApi != null) {
            return paymentResultApi;
        } else {
            paymentResultApi = getInvokeService(PaymentResultApi.class, VERSION, group);
            if(paymentResultApi != null) {
                LOGGER.info("CallBackRpcManager#getPaymentResultService get service success group:{}", group);
                paymentResultServiceMap.put(group, paymentResultApi);
                return paymentResultApi;
            }
            LOGGER.error("CallBackRpcManager#getPaymentResultService get service failed group:{}", group);
            return null;
        }
    }

    public CallbackResultApi getCallbackResultService(String group) {
        if(callbackResultApiMap.containsKey(group)) {
            return callbackResultApiMap.get(group);
        } else {
            CallbackResultApi callbackResultApi = getInvokeService(CallbackResultApi.class, VERSION, group);
            if(callbackResultApi != null) {
                LOGGER.info("CallBackRpcManager#getPaymentResultService get service success group:{}", group);
                callbackResultApiMap.put(group, callbackResultApi);
                return callbackResultApi;
            }
            LOGGER.error("CallBackRpcManager#getPaymentResultService get service failed group:{}", group);
            return null;
        }
    }

    public BusinessPayResultApi getBusinessPayResultService(String group) {
        BusinessPayResultApi businessPayResultApi = businessPayResultApiMap.get(group);
        if(businessPayResultApi != null) {
            return businessPayResultApi;
        } else {
            businessPayResultApi = getInvokeService(BusinessPayResultApi.class, VERSION, group);
            if(businessPayResultApi != null) {
                LOGGER.info("CallBackRpcManager#getBusinessPayResultService get service success group:{}", group);
                businessPayResultApiMap.put(group, businessPayResultApi);
                return businessPayResultApi;
            }
            LOGGER.error("CallBackRpcManager#getBusinessPayResultService get service failed group:{}", group);
            return null;
        }
    }
}
